<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_MenuEditAction extends m_backoffice_extends_Action
{
	public function execute()
	{
		$formValues = $this->getFormValues("form");
		
		$menu = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Menu', $this->getRequestParameter("docId"), $this->getRequestParameter("docLang"));
		
		$relManager = f_relation_Manager::getInstance();
        
		$childs = $relManager->getChildsByDocType($menu->getUniqueId(), array('modules_website/page', 'modules_website/menu'), $menu->getLang(), array("relation_order" => "ASC"), "PUBLISHED");

		foreach($childs as $key => $child)
		{
			if($child->getDocumentModel() == "modules_website/menu")
			{
				if($child->getUrl() == '')
				{
					unset($childs[$key]);
				}
			}
		}

		if($menu->isATranslation() && !$menu->isTranslated())
		{
			return $this->launchAction("website", "block_TranslateMenu");
		}

        $this->addToModel('menuLabel', $menu->getLabel());

        // to avoid notices
        $this->addToModel("errors", array('status' => null, 'label' => null, 'menuLabel' => null, 'menuHoverLabel' => null));

		if(count($formValues) > 0)
		{
            $errors = $this->checkForm($formValues);

			$menu	->setLabel($formValues["label"])
					->setMenuLabel($formValues["menuLabel"])
					->setMenuHoverLabel($formValues["menuHoverLabel"])
					->setVisibleInMenu($formValues["visibleInMenu"])
					->setVisibleInSiteMap($formValues["visibleInSiteMap"])
					->setVisibleInBreadCrumb($formValues["visibleInBreadCrumb"])
					->setPublicationstatus($formValues["publicationStatus"]);

			if(isset($formValues["indexPage"]))
			{
				$menu->setMenuPageId($formValues["indexPage"]);						
			}

            if(count($errors) == 0)
            {
                $menu->save(false);
            }
            else
            {
                $errors["status"] = "failed";
                $this->addToModel('errors', $errors);
            }
		}
		
		$this->addToModel('childs', $childs);
		$this->addToModel('menu', $menu);
		$this->setView('success');
	}	
	
	public function validate()
	{
		return $this->getUserContext()->hasRight(array("backoffice_accessBo", "website_TabPageProperties"));
	}

    private function checkForm($formValues)
    {
        $errors = array();

        if($formValues['label'] == '')
        {
            $errors['label'] = $this->getLocale('website.errors.required');
        }

        if($formValues['menuLabel'] == '')
        {
            $errors['menuLabel'] = $this->getLocale('website.errors.required');
        }

        if($formValues['menuHoverLabel'] == '')
        {
            $errors['menuHoverLabel'] = $this->getLocale('website.errors.required');
        }

        return $errors;
    }
}